<?php
namespace app\model\admin;

use think\Db;

class System
{

    //获取菜单
    public function navigation_list($where,$sort='',$paginate=10){
        $result_lists = Db::name('navigation')->where($where)->order($sort)->paginate($paginate);
        return $result_lists;
    }

    //读取菜单详情
    public function navigation_info($id){
        $result = Db::name('navigation')->where('id',$id)->find();
        return $result;
    }
    //添加菜单
    public function add_navigation($data){
        $status = Db::name('navigation')->insert($data);
        return $status;
    }

    //导航栏修改
    public function update_navigation($where,$data){
        $status = Db::name('navigation')->where($where)->update($data);
        return $status;
    }

    //导航栏删除
    public function delete_navigation($array_id){
        $navigation_delete = Db::name('navigation')->delete($array_id);
        return $navigation_delete;
    }

    //查询网站关键词、描述、logo等信息
    public function web_info()
    {
        $data = Db::name('system')->where('id',1)->find();
        return $data;
    }

    //修改网站关键词、描述、logo等信息
    public function save_web_info($data,$file='')
    {
        if(!empty($file)){//如果图片不为空则为修改
            $updimg_status = upload_file($file,'jpg,png,gif','512000','/uploads/logo');
            if($updimg_status){ //如果图片上传成功，则把图片路径写到数据库里
                $data['web_image'] = $updimg_status;
            }
        }
        $status = Db::name('system')->where('id', 1)->update($data);
        return $status ? true : false;
    }

    //修改当前管理员信息
    public function checked_admin_info($id,$password)
    {
        $status = Db::name('admin')->where('id',$id)->find();
        $password = md5(sha1(deletehtml($password)));
        if($status['password']!= $password){
            return false;
        }
        return true;
    }
    //修改当前管理员信息
    public function update_admin_info($id,$new_password)
    {
        $password = md5(sha1($new_password));
        $status = Db::name('admin')->where('id',$id)->setField('password', $password);
        return $status ? true : false;
    }

    //读取登录日志
    public function login_log(){
        //排除最新登录的一条ip
        $new_ip = Db::name('admin_login_log')->where('user_id',Session::get('admin_info.id'))->limit(1)->order('id desc')->select();
        if(Session::get('admin_info.powers')=='all'){
            $login_list = Db::name('admin_login_log')->where('id','not in',$new_ip['id'])->select();
        }else{
            $login_list = Db::name('admin_login_log')
                ->where('id','not in',$new_ip[0]['id'])
                ->where('user_id',Session::get('admin_info.id'))
                ->order('id desc')
                ->select();
        }
        if(!empty($login_list)){
            //时间戳转时间（也可在查询的时候用sql转换）
            foreach($login_list as $val){
                $val['login_time'] = date("Y-m-d H:i:s",$val['login_time']);
                $lists[] = $val;
            }
            $data['login_list'] = $lists;
            $data['now_ip'] = getIP();
            return json(['status'=>0,'data'=>$data,'msg'=>'操作成功!']);
        }else{
            return json(['status'=>1,'data'=>(object)[],'msg'=>'暂无记录!']);
        }
    }

    //获取网站收录数量
    public function baidu_web_count($web_url){
        $baidu_url = 'https://www.baidu.com/s?ie=UTF-8&wd='.$web_url.'&tn=json';
        $web_info = http_curl($baidu_url);
        return $web_info;
    }

    //获取网站访问数量
    public function web_view($date){
        $view_lists = Db::name('tongji')->where('date',$date)->select();
        return $view_lists;
    }

    //获取网站关键词排名
    //pn:从低多少条开始
    public function baidu_keyword_ranking($web_keywords,$pn){
        $baidu_url = 'https://www.baidu.com/s?ie=UTF-8&wd='.$web_keywords.'&rn=50&pn='.$pn.'&tn=json';
        $web_info = http_curl($baidu_url);
        return $web_info;
    }
}


